//
//  AccountViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/18.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "AccountViewController.h"
#import "AccountTopItem.h"
#import "AccountTableViewCell.h"
#import "ChongZhiDetailViewController.h"
#import "ChongZhiViewController.h"
#import "TiXianViewController.h"

@interface AccountViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSDictionary *personalInfoDic;
@property (nonatomic, strong) AccountTopItem *item1;
@property (nonatomic, strong) AccountTopItem *item2;

@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWhite;
    [self loadBaseTitleLable:@"账户余额"];
    self.rightBtn.hidden = NO;
    [self.rightBtn setTitle:@"充值明细" forState:(UIControlStateNormal)];
    [self.rightBtn addTarget:self action:@selector(chongZhiDetail) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self setUpUI];
    [self getPersonalInfo];
    [self getAccountList];
}
#pragma mark - 充值明细
- (void)chongZhiDetail
{
    ChongZhiDetailViewController *chongZhiDetailVC = [[ChongZhiDetailViewController alloc] init];
    [self.navigationController pushViewController:chongZhiDetailVC animated:YES];
}
#pragma mark - 获取个人信息
- (void)getPersonalInfo
{
    NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/member.ashx?action=memberinfo&companyid=%@&code=%@&timestamp=%@",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp]] parameters:nil success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        
        if ([responseObject[@"status"] intValue] == 1) {
            self.personalInfoDic = responseObject;
            
            self.item1.moneyLB.text = self.personalInfoDic[@"ZMoney"];
            self.item2.moneyLB.text = self.personalInfoDic[@"YMoney"];
        } else {
            [TFHUD addRemindHUD:self.view message:responseObject[@"returnMsg"]];
        }
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        
    }];
}
#pragma mark - 账户余额
- (void)getAccountList
{
    NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/member.ashx?action=transaction&companyid=%@&code=%@&timestamp=%@&pagesize=100000&pageindex=1",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp]] parameters:nil success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        self.dataArray = [NSMutableArray array];
        if ([responseObject[@"status"] intValue] == 1) {
            NSArray *array = responseObject[@"memberTransactionList"];
            for (NSDictionary *dic in array) {
                [self.dataArray addObject:dic];
            }
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
    }];
}
- (void)setUpUI
{
    AccountTopItem *item1 = [[AccountTopItem alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth/2, rateHeight(220))];
    item1.textLB.text = @"账户余额";
    [item1.bottomBtn setTitle:@"立即充值" forState:(UIControlStateNormal)];
    [self.view addSubview:item1];
    
    AccountTopItem *item2 = [[AccountTopItem alloc] initWithFrame:CGRectMake(kScreenWidth/2, SafeAreaTopHeight, kScreenWidth/2, rateHeight(220))];
    item2.textLB.text = @"消费总额";
    [item2.bottomBtn setTitle:@"我要提现" forState:(UIControlStateNormal)];
    [item2.bottomBtn setTitleColor:UIColorFromRGB(0x333333) forState:(UIControlStateNormal)];
    [item2.bottomBtn setBackgroundImage:[UIImage createImageWithColor:kClearColor] forState:UIControlStateNormal];
    item2.bottomBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    item2.bottomBtn.layer.borderWidth = 1;
    [self.view addSubview:item2];
    
    self.item1 = item1;
    self.item2 = item2;
    
    // 立即充值
    [self.item1.bottomBtn addTarget:self action:@selector(chongZhiAction) forControlEvents:(UIControlEventTouchUpInside)];
    // 我要提现
    [self.item2.bottomBtn addTarget:self action:@selector(tiXianAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:self.tableView];
}
#pragma mark - 充值
- (void)chongZhiAction
{
    ChongZhiViewController *chongZhiVC = [[ChongZhiViewController alloc] init];
    [self.navigationController pushViewController:chongZhiVC animated:YES];
}
#pragma mark - 提现
- (void)tiXianAction
{
    TiXianViewController *tiXianVC = [[TiXianViewController alloc] init];
    [self.navigationController pushViewController:tiXianVC animated:YES];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AccountTableViewCell *cell = [[AccountTableViewCell alloc] init];
    // 赋值
    if (_dataArray.count != 0) {
        NSDictionary *dic = _dataArray[indexPath.row];
        cell.titleLB.text = dic[@"recordType"];
        cell.detailLB.text = dic[@"operatingRecord"];
        cell.timeLB.text = [NSString stringWithFormat:@"操作时间：%@",dic[@"recordDate"]];
        cell.moneyLB.text = [NSString stringWithFormat:@"￥%@",dic[@"recordPrice"]];
    }
    cell.selectionStyle = NO;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return rateHeight(230);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return rateHeight(80);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = tableViewBgColor;
    
    UILabel *label = [UILabel labelWithText:@"账户详情" textColor:UIColorFromRGB(0x333333) fontSize:13];
    [label sizeToFit];
    [bgV addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgV);
        make.left.equalTo(bgV).offset(rateWidth(30));
    }];
    
    return bgV;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight+rateHeight(220), kScreenWidth, kScreenHeight-SafeAreaTopHeight-rateHeight(220)) style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
